﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Text;

namespace GiMess
{
    public partial class IMForm : Form
    {
        private string toUsername;
        private string fullName;
        private bool documentReady = false;

        public IMForm()
        {
            InitializeComponent();
        }

        public IMForm(string name, string username)
        {
            InitializeComponent();
            this.Text = "Chating with: " + name + " (" + username + ")";
            toUsername = username;
            fullName = name;

            webBrowser.Navigate(Application.StartupPath + "\\chat.html");
            //webBrowser.DocumentText = GiMess.Properties.Resources.chat;
            webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
            //webBrowser.IsBusy
            FillFontBox();
            
        }

        private void FillFontBox()
        {
            FontFamily[] fontFamilies;

            InstalledFontCollection installedFontCollection = new InstalledFontCollection();

            fontFamilies = installedFontCollection.Families;

            for (int i = 0; i < fontFamilies.Length; i++)
            {
                fontBox.Items.Add(fontFamilies[i].Name);
            }

            fontBox.SelectedItem = "Arial";
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            char oldc = '/';
            char newc = '\\';

            object[] path = { Application.StartupPath.Replace(newc, oldc) };
            webBrowser.Document.InvokeScript("SetPath", path);
            documentReady = true;            
        }

        public string IMUsername
        {
            get { return toUsername; }
        }

        public void Typing()
        {
            webBrowser.Invoke(new MethodInvoker(() =>
            {
                webBrowser.Document.InvokeScript("notify", new object[] { toUsername });
            }));
        }

        public void AddIMText(string text, string font)
        {
            while (!documentReady) Application.DoEvents();
            webBrowser.Invoke(new MethodInvoker(() => { 
               webBrowser.Document.InvokeScript("addMessage", AddIMTextArgs(text, font));
            }));
        }

        private object[] AddIMTextArgs(string message, string font)
        {
            object[] ret =
            {
                fullName,
                message,
                font,
                false
            };
            return ret;
        }

        private string GetFont()
        {
            string font = "";
            if (boldButton.Checked) font += "bold ";
            if (italicButton.Checked) font += "italic ";
            //if (underlineButton.Checked) font += "underline ";

            font += (string)sizeBox.SelectedItem + " ";
            font += (string)fontBox.SelectedItem;

            return font;
        }


        private void SendIM()
        {
            if (textBox2.Text != "")
            {
                //addMessage(fullName, messageText, messageFont, isSender)
                webBrowser.Document.InvokeScript("addMessage", SendIMArgs());
                //textBox1.AppendText(AuthInfo.FirstName + " " + AuthInfo.LastName + ": " + textBox2.Text + "\r\n");
                GiNetClient.Instance.SendIM(toUsername, textBox2.Text, GetFont());
                textBox2.Text = "";
            }
        }

        private object[] SendIMArgs()
        {
            object[] ret =
            {
                AuthInfo.FirstName + " " + AuthInfo.LastName,
                textBox2.Text,
                GetFont(),
                true
            };
            return ret;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendIM();
        }

        private void IMForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            FormsManager.RemoveIMForm(this);
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                SendIM();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //button2.c
        }
    }
}
